<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
use Think\Db;
class IndexController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//控制台首页
    public function index(){
        // echo md5('tui@bonnidee.cn');exit;
        // $kwd = '上海微信代运营公司';
        // // echo mb_strlen($kwd,'UTF8');exit;
        // var_dump(shoulu($kwd));exit;
        //月初
        $beginning = date('Y-m-01', strtotime(date("Y-m-d")));
        //月末
        $endMonth = date('Y-m-d', strtotime($beginning ."+1 month -1 day"));
        //上个月第一天:
        $lbeginning  = date('Y-m-d',strtotime($beginning.'-1 month')); // 计算出本月第一天再减一个月
        //上个月最后一天:
        $lendMonth =  date('Y-m-d', strtotime($beginning.' -1 day')); // 计算出本月第一天再减一天

        //上月消费
        $lmonthChargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report where log_date >= '{$lbeginning}' and log_date <= '{$lendMonth}'";

        //本月消费
        $monthChargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report where log_date >= '{$beginning}'";

        //今日消费
        $date = date("Y-m-d",time());
        $datesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report where log_date= '{$date}'";

        //昨日消费
        $prvedate = date("Y-m-d",strtotime('-1 day'));
        $prvedatesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report where log_date= '{$prvedate}'";
        //总消费
        $totalchargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report";

        $newcharge =  M()->query($datesql);
        $prvecharge =  M()->query($prvedatesql);
        $monthCharge =  M()->query($monthChargesql);
        $lmonthCharge =  M()->query($lmonthChargesql);
        $totalcharge =  M()->query($totalchargesql);

        //上月每日任务
        $wherelinep['log_date'] = array('egt',$lbeginning);
        $wherelinep['log_date'] = array('elt',$lendMonth);
        $linep = M('daily_report')->where($wherelinep)->Field('log_date,sum(standard) as num')->group('log_date')->select();
        //上月每日消费
        $linep2 = M('daily_report')->where($wherelinep)->Field('log_date,sum(pay_amount) as num')->group('log_date')->select();

        //本月每日任务
        $wherelinen['log_date'] = array('egt',$beginning);
        $linen = M('daily_report')->where($wherelinen)->Field('log_date,sum(standard) as num')->group('log_date')->select();
        //本月每日消费
        $linen2 = M('daily_report')->where($wherelinen)->Field('log_date,sum(pay_amount) as num')->group('log_date')->select();

        foreach ($linep as $key => $v) {
            $linp[] = $v['num'];
        }

        foreach ($linep2 as $key => $v) {
            $linp2[] = $v['num'];
        }

        foreach ($linen as $key => $v) {
            $linn[] = $v['num'];
        }

        foreach ($linen2 as $key => $v) {
            $linn2[] = $v['num'];
        }
        // var_dump($linep);exit;


        if(count($linp) == 0){
           $linp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linn) == 0){
            $linn = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linp2) == 0){
            $linp2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linn2) == 0){
            $linn2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        $linelist = array(
            'line' =>  array(
                // 'linep' => [12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3,3],//$linep
                'linep' => $linp,
                // 'linen' => [11, 13, 13, 7, 12, 3, 18, 33, 25, 13, 30, 10,22, 19, 3, 5, 6, 7, 20, 23, 23, 15, 30, 10,18, 15, 13, 8, 4, 5,5],//$linen
                'linen' => $linn,
            ),
            'line2' =>  array(
                // 'linep' => [12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3,3],//$linep2
                'linep' => $linp2,
                // 'linen' => [11, 13, 13, 7, 12, 3, 18, 33, 25, 13, 30, 10,22, 19, 3, 5, 6, 7, 20, 23, 23, 15, 30, 10,18, 15, 13, 8, 4,5,5],//$linen2,
                'linen' => $linn2,
            ),
        );
        $line = json_encode($linelist);


        $header = array(
            '本月消费' => '￥'.floatval($monthCharge[0]['newcharge']),
            '今日消费' => '￥'.floatval($newcharge[0]['newcharge']),
            '客户量' => intval($totalcharge[0]['customer']),
            '当日任务数' => intval($newcharge[0]['task']),
            '当日达标关键词数量' => intval($newcharge[0]['standards']),
            '当日达标率' => round($newcharge[0]['standards']/$newcharge[0]['task']*100,2).'％',
        );

    
        $task = array(
            '总达标' => intval($totalcharge[0]['standards']),
            '上月' => intval($lmonthCharge[0]['standards']),
            '本月' => intval($monthCharge[0]['standards']),
            '昨日' => intval($prvecharge[0]['standards']),
        );
        $amount = array(
            '总消费' => '￥'.floatval($totalcharge[0]['newcharge']),
            '上月消费' => '￥'.floatval($lmonthCharge[0]['newcharge']),
            '本月消费' => '￥'.floatval($monthCharge[0]['newcharge']),
            '昨日消费' => '￥'.floatval($prvecharge[0]['newcharge']),
        );
        // var_dump($header);exit;

        $whereadmin['status'] = 1;
        $adminlist = M('admin')->where($whereadmin)->getfield('admin_id,user_name');
        $count = count($adminlist);
        foreach ($adminlist as $key => $v) {
            $desc[] = '代理商'.$v.'已欠费,请及时充值';
        }
        $this->assign("line", $line);
        $this->assign("header", $header);
        $this->assign("task", $task);
        $this->assign("amount", $amount);
        $this->assign("count", $count);
        $this->assign("desc", $desc);
        $this->display();
    }


    

    

    



    //代理商
    public function handle(){
        $res = I('');
        if(!$_FILES['logo'] || !$_FILES['ico']){
            $this->error('请上传logo与ico');exit;
        }
        if(!$res['user_name'] || !$res['password'] || !$res['websites'] || !$res['company_name'] || !$res['phone'] || !$res['qq'] || !$res['email'] || !$res['icp'] || !$res['address']){
            $this->error('请填写完整信息');
        }
       
        $data = $this->upload();
        $res['logo'] = $data['logo'];
        $res['ico'] = $data['ico'];
        $res['add_time'] = time();
        $res['update_time'] = time();
        $res['type'] = 1;
        $res['permissions'] = json_encode($res['permissions']);
        $list = M('admin')->add($res);
        if($list){
            $this->success("添加成功", U("Index/pttable"));exit;
        }else{
            $this->error("添加失败");exit;
        }

        $this->display();
    }




     public function ptindex(){
        //月初
        $beginning = date('Y-m-01', strtotime(date("Y-m-d")));
        //月末
        $endMonth = date('Y-m-d', strtotime($beginning ."+1 month -1 day"));
        //上个月第一天:
        $lbeginning  = date('Y-m-d',strtotime($beginning.'-1 month')); // 计算出本月第一天再减一个月
        //上个月最后一天:
        $lendMonth =  date('Y-m-d', strtotime($beginning.' -1 day')); // 计算出本月第一天再减一天

        //上月消费
        $lmonthChargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report where log_date >= '{$lbeginning}' and log_date <= '{$lendMonth}'";

        

        //本月消费
        $monthChargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report_user where log_date >= '{$beginning}'";


        //今日消费
        $date = date("Y-m-d",time());
        $datesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report_user where log_date= '{$date}'" ;
        // echo $datesql;exit;
        //昨日消费
        $prvedate = date("Y-m-d",strtotime('-1 day'));
        $prvedatesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report_user where log_date= '{$prvedate}'";
        //总消费
        $totalchargesql = "select sum(pay_amount) as newcharge,count(distinct admin_id) as customer,sum(keyworlds) as task,sum(standard) as standards from pt_daily_report";

        //总消费
        $sitesql = "select sum(sitecount) as site from pt_daily_report pt_daily_report where log_date= '{$date}'";

        $newcharge =  M()->query($datesql);
        $prvecharge =  M()->query($prvedatesql);
        $monthCharge =  M()->query($monthChargesql);
        $lmonthCharge =  M()->query($lmonthChargesql);
        $totalcharge =  M()->query($totalchargesql);
        $site =  M()->query($sitesql);

        //上月每日任务
        $wherelinep['log_date'] = array('egt',$lbeginning);
        $wherelinep['log_date'] = array('elt',$lendMonth);
        $linep = M('daily_report')->where($wherelinep)->Field('log_date,sum(standard) as num')->group('log_date')->select();
        //上月每日消费
        $linep2 = M('daily_report')->where($wherelinep)->Field('log_date,sum(pay_amount) as num')->group('log_date')->select();

        //本月每日任务
        $wherelinen['log_date'] = array('egt',$beginning);
        $linen = M('daily_report')->where($wherelinen)->Field('log_date,sum(standard) as num')->group('log_date')->select();
        //本月每日消费
        $linen2 = M('daily_report')->where($wherelinen)->Field('log_date,sum(pay_amount) as num')->group('log_date')->select();


        //
        // var_dump($linep);exit;


        foreach ($linep as $key => $v) {
            $linp[] = $v['num'];
        }

        foreach ($linep2 as $key => $v) {
            $linp2[] = $v['num'];
        }

        foreach ($linen as $key => $v) {
            $linn[] = $v['num'];
        }

        foreach ($linen2 as $key => $v) {
            $linn2[] = $v['num'];
        }
        // var_dump($linep);exit;


        if(count($linp) == 0){
           $linp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linn) == 0){
            $linn = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linp2) == 0){
            $linp2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        if(count($linn2) == 0){
            $linn2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0,0];
        }
        $linelist = array(
            'line' =>  array(
                // 'linep' => [12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3,3],//$linep
                'linep' => $linp,
                // 'linen' => [11, 13, 13, 7, 12, 3, 18, 33, 25, 13, 30, 10,22, 19, 3, 5, 6, 7, 20, 23, 23, 15, 30, 10,18, 15, 13, 8, 4, 5,5],//$linen
                'linen' => $linn,
            ),
            'line2' =>  array(
                // 'linep' => [12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3, 20, 33, 23, 12, 33, 10,12, 19, 3, 5, 2, 3,3],//$linep2
                'linep' => $linp2,
                // 'linen' => [11, 13, 13, 7, 12, 3, 18, 33, 25, 13, 30, 10,22, 19, 3, 5, 6, 7, 20, 23, 23, 15, 30, 10,18, 15, 13, 8, 4,5,5],//$linen2,
                'linen' => $linn2,
            ),
        );
        $line = json_encode($linelist);

        // $header = array(
        //     '站点数量' => 18,
        //     '客户数量' => 15,
        //     '今日消费' => '￥',
        //     '当日任务数' => 411,
        //     '当日达标关键词数量' =>358 ,
        //     '当日达标率' => round(358/411*100,2).'％',
        // );

        // $task = array(
        //     '总达标' => 2500,
        //     '上月' => 720,
        //     '本月' => 320,
        //     '昨日' => 120,
        // );
        // $amount = array(
        //     '总消费' => '￥354000',
        //     '上月消费' => '￥54000',
        //     '本月消费' => '￥14000',
        //     '昨日消费' => '￥1300',
        // );


        $header = array(
            '站点数量' => intval($site[0]['site']),
            '客户数量' => intval($totalcharge[0]['customer']),
            '今日消费' => '￥'.floatval($newcharge[0]['newcharge']),
            '当日任务数' => intval($newcharge[0]['task']),
            '当日达标关键词数量' => intval($newcharge[0]['standards']),
            '当日达标率' => round($newcharge[0]['standards']/$newcharge[0]['task']*100,2).'％',
        );

        $task = array(
            '总达标' => intval($totalcharge[0]['standards']),
            '上月' => intval($lmonthCharge[0]['standards']),
            '本月' => intval($monthCharge[0]['standards']),
            '昨日' => intval($prvecharge[0]['standards']),
        );
        $amount = array(
            '总消费' => '￥'.floatval($totalcharge[0]['newcharge']),
            '上月消费' => '￥'.floatval($lmonthCharge[0]['newcharge']),
            '本月消费' => '￥'.floatval($monthCharge[0]['newcharge']),
            '昨日消费' => '￥'.floatval($prvecharge[0]['newcharge']),
        );




        $whereadmin['status'] = 1;
        $adminlist = M('admin')->where($whereadmin)->getfield('admin_id,user_name');
        $count = count($adminlist);
        foreach ($adminlist as $key => $v) {
            $desc[] = '代理商'.$v.'已欠费,请及时充值';
        }
        $this->assign("line", $line);
        $this->assign("header", $header);
        $this->assign("task", $task);
        $this->assign("amount", $amount);
        $this->assign("count", $count);
        $this->assign("desc", $desc);
        layout('Layout/twolayout');
        $this->display();
    }

    

    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();

        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $files[$file['key']] =  '/Uploads/'.$file['savepath'].$file['savename'];
            }
        }

        return $files;
    }
}